package offer;

/**
 * 剑指 Offer 24. 反转链表
 */
public class ReverseList {
    public int num = 0;

    public static void main(String[] args) {
        DeleteNode.ListNode listNode = new DeleteNode.ListNode(1);
        listNode.next = new DeleteNode.ListNode(2);
        listNode.next.next = new DeleteNode.ListNode(3);
        listNode.next.next.next = new DeleteNode.ListNode(4);
//        DeleteNode.ListNode res = reverseList(listNode);
        DeleteNode.ListNode res = new ReverseList().recursionReverseList(listNode);
    }

    public static DeleteNode.ListNode reverseList(DeleteNode.ListNode head) {
        DeleteNode.ListNode curr = head;
        DeleteNode.ListNode pre = null;
        while (curr != null) {
            DeleteNode.ListNode tmp = curr.next;
            curr.next = pre;
            pre = curr;
            curr = tmp;
        }
        return pre;
    }

    public DeleteNode.ListNode recursionReverseList(DeleteNode.ListNode head) {
        DeleteNode.ListNode recursion = recursion(head, null);
        return recursion;
    }

    public DeleteNode.ListNode recursion(DeleteNode.ListNode cur, DeleteNode.ListNode pre) {
        //结束条件
        if (cur == null) {
            return pre;
        }
        DeleteNode.ListNode res = recursion(cur.next, cur);
        cur.next = pre;
        return res;
    }

}
